package fly.text;

import fly.help.ArraySupport;
import fly.help.PrintSupport;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

public class DecimalFormatDemo {



    public static void main(String[] args) {
        PrintSupport.sout("1");
        NumberFormat nf = NumberFormat.getInstance(Locale.US);
        if( nf instanceof DecimalFormat)
        {
            ((DecimalFormat) nf).setDecimalSeparatorAlwaysShown(true);
            System.out.println(nf.toString());
        }

        PrintSupport.sout("2");
        DecimalFormat df = new DecimalFormat();
        String[] strArr = {
                df.getNegativePrefix(), df.getNegativeSuffix(), df.getPositivePrefix(), df.getPositiveSuffix(),
                df.toLocalizedPattern(), df.toPattern()
        };
        System.out.println(
                ArraySupport.toString(strArr)
        );
        try {
            System.out.println( df.parse("324.230"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        PrintSupport.sout("3");
        String pattern = "####0.###";
        DecimalFormat dfp = new DecimalFormat(pattern);
        String[] strArrp = {
                dfp.getNegativePrefix(), dfp.getNegativeSuffix(), dfp.getPositivePrefix(), dfp.getPositiveSuffix(),
                dfp.toLocalizedPattern(), dfp.toPattern()
        };
        System.out.println(
                ArraySupport.toString(strArrp)
        );
        try {
            System.out.println( dfp.parse("324.23")); // ?
        } catch (ParseException e) {
            e.printStackTrace();
        }
        PrintSupport.sout("4");
        Locale[] locales = NumberFormat.getAvailableLocales();
        System.out.println(
                ArraySupport.toString(
                  locales
                )
        );
        double db = -1234.56;
        NumberFormat nft;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < locales.length; j++) {
                if(locales[j].getCountry().length() == 0)
                {
                    continue;
                }

                System.out.println( locales[j].getDisplayName() );
                switch (i){
                    case 0:
                        nft = NumberFormat.getInstance( locales[j] );
                        break;
                    case 1:
                        nft = NumberFormat.getIntegerInstance( locales[j]);
                        break;
                    case 2:
                        nft = NumberFormat.getCurrencyInstance( locales[j]);
                        break;
                    default:
                        nft = NumberFormat.getPercentInstance( locales[j]);
                        break;
                }

                if( nft instanceof DecimalFormat){
                    System.out.println(": " + ((DecimalFormat) nft).toPattern());
                }
                System.out.println(" -> " + nft.format(db));
                try {
                    System.out.println(" -> " + nft.parse(nft.format(db)));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
